// pages/index/detail/detail.js
import { getRemindCardInformation, delRemind } from '../../../api/index'
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
Page({
  data:{
    remind:{ 
    drugDays: "4",
    drugDuring: "2",
    drugName: "阿司匹林",
    drugNumber: "2",
    drugSpace: "3",
    note: "记得按时吃药"
  }
  },
  onLoad(options){
    this.setData({id:options.id})
  },

  onShow(){
    getRemindCardInformation({id:this.data.id})
      .then(res=> {
        let { remind } = res;
        let { drugSpace } = remind;
        drugSpace == '0' ? drugSpace = '每天' : `隔${drugSpace}天吃`
        remind.drugSpace = drugSpace
        this.setData({remind})
      })
  },
  //点击修改按钮
  changeDetail(){
    wx.navigateTo({
      url:'../add?page=detailPage&id='+this.data.id
    })
  },

  // 点击按钮后弹出弹框确认是否删除
  delete(){
    let id = this.data.id;
    Dialog.confirm({
      title: '提示',
      message: '是否删除该提醒',
    })
      .then(() => {
        // 点击确认后发送删除请求
        console.log('yes')
        delRemind({id})
          .then(res=>{
            let { State } = res;
            if(State){
              Dialog.alert({
                message: '删除成功,点击返回首页查看',
              }).then(() => {
                // on close
                wx.navigateBack({delta: 1})
              });
            }else{
              Toast.fail('删除失败，请重试')
            }
          })
      })
      .catch(() => {
        // on cancel
      });
  }
})